package com.example.imiaow.yyads_text;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;


import com.youyi.yesdk.YOUEAdSdk;
import com.youyi.yesdk.ad.FullVideoAd;
import com.youyi.yesdk.ad.RewardVideoAd;
import com.youyi.yesdk.ad.YOUEAdConstants;
import com.youyi.yesdk.business.AdPlacement;
import com.youyi.yesdk.business.YOUEAdManager;
import com.youyi.yesdk.listener.FullVideoListener;
import com.youyi.yesdk.listener.RewardListener;

import org.jetbrains.annotations.Nullable;

import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** YyadsTextPlugin */
public class YyadsTextPlugin implements FlutterPlugin, MethodCallHandler ,ActivityAware {
  private MethodChannel channel;
  private Context context;
  private Activity mactivity;
  private FlutterPluginBinding mFlutterPluginBinding;
  private RewardVideoAd mrewardVideoAd;
  private FullVideoAd mfullVideoAd;
  private boolean getRewaredScuess = false;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "yyads_text");
    channel.setMethodCallHandler(this);
    context = flutterPluginBinding.getApplicationContext();
    mFlutterPluginBinding = flutterPluginBinding;
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
     if(call.method.equals("init")){
        YOUEAdSdk.INSTANCE.initSDK(
                context,
                new YOUEAdManager()
                        .appName("闪金喵")
                        .appId("000182")
                        .deBug(false)
                        .supportMultiProcess(false)
                        .setChannel(10001)
                        .build());
    }
    else if (call.method.equals("getVersion")){
      String j;
      j = YOUEAdSdk.INSTANCE.getSDKVersion();
      result.success(j);
    }
    else if(call.method.equals("getRewardVideoAd")){
        float width = Float.valueOf(call.argument("width"));
        float height = Float.valueOf(call.argument("height"));
        mrewardVideoAd = new RewardVideoAd();
        mrewardVideoAd.setRewardConfig(mactivity,new AdPlacement
                .Builder()
                .setAdId("0000000477")
                .setExpressViewAcceptedSize(width,height)
                .setOrientation(YOUEAdConstants.VERTICAL)
                .setScenes(YOUEAdConstants.RitScenes.CUSTOMIZE_SCENES,"flutter_scenes")
                .build()
        ).loadRewardVideo(new RewardListener() {
          //广告请求出错
          @Override
          public void onError(@Nullable Integer integer, @Nullable String s) {
            result.success(false);
          }

          //广告以缓存，此时可调用 show() 方法展示广告
          @Override
          public void onVideoCached() {
//            mrewardVideoAd.show();
          }
          //广告已加载完成
          @Override
          public void onADLoaded() {
          }
          //广告已曝光
          @Override
          public void onADShow() {
          }
          //奖励发放回调
          @Override
          public void onReward(@Nullable Boolean aBoolean, @Nullable Integer integer, @Nullable String s, @Nullable Integer integer1, @Nullable String s1, @Nullable Map<String, Object> map) {
            getRewaredScuess = aBoolean;
          }
          //视频播放完成回调
          @Override
          public void onADComplete() {
          }
          //广告下载bar点击回调
          @Override
          public void onVideoBarClick() {
          }
          //跳过视频播放回调
          @Override
          public void onSKipVideo() {
          }
          //广告关闭回调
          @Override
          public void onClosed() {
            result.success(getRewaredScuess);
            mrewardVideoAd.destroy();
          }
        });

     }
    else if(call.method.equals("showreward")){
         mrewardVideoAd.show();
     }
    else if(call.method.equals("getFullVideoAd")){
      mfullVideoAd= new FullVideoAd();
      mfullVideoAd.setVideoConfig(mactivity,new AdPlacement
              .Builder()
              .setAdId("0000000476")
              .setExpressViewAcceptedSize(800,800)
              .setOrientation(YOUEAdConstants.VERTICAL)
              .build()
      );
      mfullVideoAd.loadFullVideo(new FullVideoListener() {
        //广告加载完成
        @Override
        public void onAdLoaded() {
        }
        //广告已缓存到本地，可调用 show() 展示广告
        @Override
        public void onAdCached() {
          mfullVideoAd.show();
        }
        //广告展示回调
        @Override
        public void onAdShow() {
        }
        //广告跳过回调
        @Override
        public void onAdSkipped() {
        }
        //广告点击回调
        @Override
        public void onAdClicked() {
        }
        //视频广告播放完成回调
        @Override
        public void onAdComplete() {
        }
        //广告关闭回调
        @Override
        public void onAdClosed() {
          result.success(true);
        }
        //广告请求错误，返回错误码及错误信息
        @Override
        public void onError(@Nullable Integer integer, @Nullable String s) {
          result.success(false);
        }
      });

     }
    else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.mactivity = binding.getActivity();
    ViewPlugin viewPlugin = new ViewPlugin();
    viewPlugin.registerWith(mFlutterPluginBinding,mactivity);
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
  }

  @Override
  public void onDetachedFromActivity() {

  }


}
